from django.db import models
from django.conf import settings

# Create your models here.

class Blog(models.Model):
    '''站点表'''
    site_name = models.CharField(verbose_name= '站点名称',max_length=16)
    site_desc = models.CharField(verbose_name= '博客简介',max_length= 255)
    visitor_volume = models.IntegerField(verbose_name= '被访问量/点击量',default= 0)
    user = models.ForeignKey(
        to= settings.AUTH_USER_MODEL,
        on_delete= models.CASCADE,
        db_constraint= False
        )

    class Meta:
        verbose_name = '站点表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.site_name


class Category(models.Model):
    '''文章分类表'''
    name = models.CharField(verbose_name= '文章分类',max_length= 32)

    class Meta:
        verbose_name = '文章分类表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name



class Tag(models.Model):
    '''文章标签表'''
    name = models.CharField(verbose_name= '文章标签',max_length= 32)
    category = models.ForeignKey(
        to= 'Category',
        on_delete= models.CASCADE,
        db_constraint= False
        )

    class Meta:
        verbose_name = '文章标签表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Article(models.Model):
    '''文章表'''
    type_choices = (
        (0, '原创'),
        (1, '转载')
        )

    title = models.CharField(verbose_name= '文章标题',max_length= 64)
    desc = models.CharField(verbose_name= '文章描述',max_length= 255)
    type = models.IntegerField(verbose_name= '文章类型',choices= type_choices)
    content = models.TextField(verbose_name= '文章内容')
    create_time = models.DateTimeField(verbose_name= '创建时间',auto_now_add= True)

    visitor_num = models.IntegerField(verbose_name= '访问量', default= 0)
    comment_num = models.IntegerField(verbose_name= '评论量', default= 0)
    up_num = models.IntegerField(verbose_name= '点赞数', default= 0)
    down_num = models.IntegerField(verbose_name= '点踩数', default= 0)

    blog = models.ForeignKey(
        to= 'blog',
        on_delete= models.CASCADE,
        db_constraint= False
        )
    category = models.ForeignKey(
        to= 'Category',
        on_delete= models.SET_NULL,
        db_constraint= False,
        null= True,
        blank= True
        )
    tags = models.ManyToManyField(
        to= 'Tag',
        through= 'Article2Tag',
        through_fields= ['article', 'tag']
        )

    class Meta:
        verbose_name = '文章表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

    @property
    def type_name(self):
        '''返回文章类型可读形式'''
        return self.get_type_display()

    @property
    def type_link(self):
        '''根据文章类型，返回对应图片的链接'''
        type = self.get_type_display()
        if type == '原创':
            return 'img/type/original.png'
        elif type == '转载':
            return 'img/type/reprint.png'

class Article2Tag(models.Model):
    article = models.ForeignKey(
        to= 'Article',
        on_delete= models.CASCADE
        )
    tag = models.ForeignKey(
        to= 'Tag',
        on_delete= models.CASCADE
        )


class Comment(models.Model):
    '''评论表'''
    user = models.ForeignKey(
        to= settings.AUTH_USER_MODEL,
        on_delete= models.DO_NOTHING,
        db_constraint= False,
        )
    article = models.ForeignKey(
        to= 'Article',
        on_delete= models.CASCADE,
        db_constraint= False
        )
    content = models.CharField(verbose_name= '评论内容',max_length= 255)
    create_time = models.DateTimeField(verbose_name= '评论时间',auto_now_add= True)
    parent = models.ForeignKey(
        to = 'self',
        verbose_name= '是否父评论',
        on_delete= models.CASCADE,
        db_constraint= False,
        null= True, blank= True
        )

    class Meta:
        verbose_name = '评论表'
        verbose_name_plural = verbose_name


class UpAndDown(models.Model):
    '''点赞点踩表'''
    user = models.ForeignKey(
        to= settings.AUTH_USER_MODEL,
        on_delete= models.DO_NOTHING,
        db_constraint= False,
        )
    article = models.ForeignKey(
        to= 'Article',
        on_delete= models.CASCADE,
        db_constraint= False
        )
    is_up = models.BooleanField(verbose_name= '点赞点踩')

    class Meta:
        verbose_name = '点赞点踩表'
        verbose_name_plural = verbose_name
